import { Component } from '@angular/core';
import { IonicPage, NavController, ViewController, NavParams } from 'ionic-angular';
import { HttpProvider } from '../../providers/http/http';
import { StorageProvider } from '../../providers/storage/storage';
import { DialogProvider } from '../../providers/dialog/dialog';
import { UserInfoProvider } from '../../providers/user-info/user-info';
import { user_info } from '../login/login';
import { ChargDataService } from '../../providers/service/charg-data.service';
import { Network } from '@ionic-native/network';


/**
 * Generated class for the ChargingListPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-charging-list',
  templateUrl: 'charging-list.html',
})
export class ChargingListPage {

  member_id: any;

  page = 1;

  //充电记录
  public charglist: Charg[] = new Array<Charg>();
  public charglistold: Charg[] = new Array<Charg>();

  //设备原始充电记录
  public Rawdevice: rawdevices[] = new Array<rawdevices>();
  //用户信息
  userInfo: user_info = new user_info();
  //有网状态=1 断网=0
  Internet = 1;
  // 下拉是否还有数据
  nodata = 0;



  constructor(
    public navCtrl: NavController,
    public viewCtrl: ViewController,
    public http: HttpProvider,
    public storage: StorageProvider,
    public dialog: DialogProvider,
    public chargdata: ChargDataService,
    public user: UserInfoProvider,
    private network: Network, ) { }

  async ionViewDidLoad() {
    console.log('ionViewDidLoad ChargingListPage');
    // this.storage.getStorage("login_info").then(res => {
    //   if (res != undefined) {
    //     this.member_id = res['member_id']
    //   }
    //   this.getcharging();
    // });
  }

  async ionViewDidEnter() {
    if (this.network.type !== 'none' && this.network.type !== '2g') {
      this.Internet = 1;
      this.userInfo = this.user.getuserInfo();
      this.member_id = this.userInfo.member_id;
      this.getcharging();
    }
    if (this.network.type === 'none' || this.network.type === '2g') {
      this.Internet = 0;
      let a = await this.chargdata.find((chargdata) => chargdata.key !== '');
      this.getMessage(a);
    }
  }

  skipToComplain(data) {
    if (this.network.type === 'none' || this.network.type === '2g') {
      this.dialog.showToast('提示', '当前无网络无法使用改功能');
      return;
    }
    this.navCtrl.push("ComplainPage", { data: data });
  }

  async getcharging() {
    this.http.postJson('', 'member/memberch/index?page=' + this.page,
      {
        "where": ["and", ["=", "m_member_charge.member_id", this.member_id], ["=", "m_member_charge.states", "1"]],
        "distinct": true,
        "limit": 10,
        "joinwith": ["q_charging_pile"],
        "orderBy": "charge_time_start DESC"
      }, '').then(res => {
        if (res['status'] == 200) {
          this.getMessage(res['data']);
        }
        if (res['data'].length < 6) {
          this.nodata = 1;
        }
        if (res['data'].length === 6) {
          this.nodata = 0;
        }
      });
  }

  async getMessage(charglist) {
    this.charglist.splice(0);
    this.charglistold.splice(0);
    for (let listcharg of charglist) {
    listcharg.remaining_time = Math.floor( ( new Date(listcharg.charge_time_end * 1000).getTime() - new Date().getTime() ) / 60000)
      if (new Date(listcharg.charge_time_start * 1000).getMonth() < new Date().getMonth()) {
        this.charglistold.push(listcharg);
      } else {
        this.charglist.push(listcharg);
      }
    }
    this.chargdata.deleteAll();
    for (var i = 0; i < this.charglist.length; i++) {
      this.chargdata.save(this.charglist[i]);
    }
    for (var j = 0; j < this.charglistold.length; j++) {
      this.chargdata.save(this.charglistold[j]);
    }
  }

  lode(infiniteScroll, type) {
    this.page++;
    this.http.postJson('', 'member/memberch/index?page=' + this.page,
      {
        "where": ["and", ["=", "m_member_charge.member_id", this.member_id], ["=", "m_member_charge.states", "1"]],        
        "distinct": true,
        "limit": 6,
        "joinwith": ["q_charging_pile"],
        "orderBy": "charge_time_start DESC"
      }, '').then(res => {
        if (res['status'] == 200) {

          if (res['data'].length === 0) {
            this.nodata = 1;
          }

          for (let listcharg of res['data']) {
            if (new Date(listcharg.charge_time_start * 1000).getMonth() < new Date().getMonth()) {
              this.charglistold.push(listcharg);
            } else {
              this.charglist.push(listcharg);
            }
          }
          if (type === 'scroll') {
            setInterval(infiniteScroll.complete(), 500);
          }
        }
        //      infiniteScroll.complete();
      });

  }

  //设备原始充电记录接口
  Rawdevices() {
    var params = {
      batchMTC:
      [

        {
          "charge_time_start": "1522303257",
          "charge_time_end": "1522321257",
          "charge_time": "4",
          "power_unit_id": "1",
          "member_id": this.member_id
        }

      ]

    }
    this.http.postJson('', 'business/chargingpr/create',
      params, '')
      .then(res => {
        if (res['status'] == 200) {
          //this.dialog.showAlert("学霸", res['message']);
        }
      });
  }



}


export class Charg {
  member_charge_id: any;
  charge_code: any;
  charge_time_start: any;
  charge_time_end: any;
  charge_time: any;
  power_unit_id: any;
  member_id: any;
  payment_money: any;
  discount_money: any;
  refund_money: any;
  order_money: any;
  payment_id: any;
  states: any;
  created_at: any;
  updated_at: any;
  cuser_id: any;
  uuser_id: any;
  tables: any;
  q_charging_pile: {
    charging_pile_id: any;
    charging_pile_name: any;
    cell_name: any;
  }
  remaining_time: any;
}

export class rawdevices {
  charge_time_start: any;
  charge_time_end: any;
  charge_time: any;
  power_unit_id: any;
  member_id: any;
  created_at: any;
  updated_at: any;
  cuser_id: any;
  uuser_id: any;
  record_id: any;

}

